package com.express.e.root.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter({"/admin/courier/*","/admin/delivery/*","/admin/user/*","*.jsp"})
public class EncodingFileter implements Filter {
    private HttpServletRequest request = null;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("___________                                            ___________ \n" +
                "\\_   _____/__  ________________   ____   ______ ______ \\_   _____/ \n" +
                " |    __)_\\  \\/  /\\____ \\_  __ \\_/ __ \\ /  ___//  ___/  |    __)_  \n" +
                " |        \\>    < |  |_> >  | \\/\\  ___/ \\___ \\ \\___ \\   |        \\ \n" +
                "/_______  /__/\\_ \\|   __/|__|    \\___  >____  >____  > /_______  / \n" +
                "        \\/      \\/|__|               \\/     \\/     \\/          \\/  ");
        System.out.println("快递E栈已经启动啦~  *\\(^o^)/*" );
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding("utf-8");
        servletResponse.setCharacterEncoding("utf-8");

        request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        if (null != request.getSession().getAttribute("username")
                ||null != request.getSession().getAttribute("usersname")
                ||null != request.getSession().getAttribute("adminName")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }
        response.getWriter().write("<script>alert('please login first! url: /login.jsp');</script>");
    }

    @Override
    public void destroy() {
    }
}
